using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TemplatePlugin
{
    public partial class ChooseTemplateForm : Form
    {
        public ChooseTemplateForm()
        {
            InitializeComponent();
            RefreshTemplates();
        }

        private void RefreshTemplates()
        {
            // load all the templates from disk
            DirectoryInfo di = new DirectoryInfo(TemplatePlugin.PluginsFolder);
            if (di.Exists)
            {
                this.templateListBox.Items.Clear();
                FileInfo[] files = di.GetFiles();
                this.templateListBox.Items.AddRange(files);
            }
            else
            {
                di.Create();
            }
        }

        public string Template
        {
            get 
            {
                // read in the template and return it's contents
                string result = string.Empty;
                if (this.templateListBox.SelectedIndex >= 0)
                {
                    StreamReader sr = ((FileInfo)this.templateListBox.SelectedItem).OpenText();
                    result = sr.ReadToEnd();
                }
                return result;
            }
        }

        private void templateListBox_DoubleClick(object sender, EventArgs e)
        {
            // quick close the dialog
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void createTemplateButton_Click(object sender, EventArgs e)
        {
            using (CreateTemplateForm createTemplateForm = new CreateTemplateForm())
            {
                DialogResult result = createTemplateForm.ShowDialog();
                if (result == DialogResult.OK)
                {
                    this.RefreshTemplates();
                }
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (this.templateListBox.SelectedIndex >= 0)
            {
                if (MessageBox.Show(string.Format("Are you sure you want to delete {0}?", ((FileInfo)this.templateListBox.SelectedItem).Name), "Confirm deletion", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                {
                    ((FileInfo)this.templateListBox.SelectedItem).Delete();
                    this.RefreshTemplates();
                }
            }
        }

        private void modifyButton_Click(object sender, EventArgs e)
        {
            if (this.templateListBox.SelectedIndex >= 0)
            {
                using (CreateTemplateForm createTemplateForm = new CreateTemplateForm((FileInfo)this.templateListBox.SelectedItem))
                {
                    DialogResult result = createTemplateForm.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        this.RefreshTemplates();
                    }
                }
            }
        }
    }
}